package cn.lili.controller.payment;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.kit.CashierSupport;
import cn.lili.modules.payment.entity.dto.PayParam;
import cn.lili.modules.payment.entity.enums.PaymentMethodEnum;
import cn.lili.modules.payment.entity.vo.ApplePaymentVO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 买家端,收银台接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@RestController
@Api(tags = "买家端,收银台接口")
@RequestMapping("/payment/cashier")
@RequiredArgsConstructor
public class CashierController {

    private final CashierSupport cashierSupport;

    @ApiOperation(value = "支付回调")
    @RequestMapping(value = "/callback/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Object> callback(@PathVariable String paymentMethod) throws Exception {

        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);

        cashierSupport.callback(paymentMethodEnum);

        return ResultUtil.success();
    }

    @ApiOperation(value = "支付宝支付异步通知")
    @RequestMapping(value = "/notify/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public String  notify( @PathVariable String paymentMethod) throws Exception {

        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.paymentNameOf(paymentMethod);

        cashierSupport.notify(paymentMethodEnum );
        return "success";
    }

    @ApiOperation(value = "微信支付异步通知")
    @RequestMapping(value = "/notifyWechat/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public String  notifyWechat( @PathVariable String paymentMethod) throws Exception {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.paymentNameOf(paymentMethod);

        cashierSupport.notify(paymentMethodEnum );

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SUCCESS",200);
        return jsonObject.toJSONString();
    }

    @ApiOperation(value = "苹果支付异步通知")
    @RequestMapping(value = "/notifyApple/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Object> notifyApple( @PathVariable String paymentMethod, @RequestBody ApplePaymentVO applePaymentVO) {
        log.info("苹果支付异步通知");
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.paymentNameOf(paymentMethod);

        assert paymentMethodEnum != null;
        cashierSupport.notify(paymentMethodEnum, applePaymentVO);

        return ResultUtil.success();
    }

    @ApiOperation(value = "查询支付结果")
    @GetMapping(value = "/result")
    public ResultMessage<Boolean> paymentResult(String sn) {
        return ResultUtil.data(cashierSupport.paymentResult(sn));
    }
}
